package com.zzyl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description DeviceDataServiceImpl
 * @Author luohai
 * @Date 2024-11-02
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    /**
     * 获取物模型数据分页结果
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        LocalDateTime StartDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime EndDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());
        deviceDataPageReqDto.setStartDate(StartDate);
        deviceDataPageReqDto.setEndDate(EndDate);
        Page<DeviceDataVo> page=deviceDataMapper.getDeviceDataPage(deviceDataPageReqDto);
        return PageResponse.of(page,DeviceDataVo.class);
    }

    /**
     * 按日查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        List<DeviceDataGraphVo> deviceDataGraphVoList1 = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        if (CollectionUtil.isNotEmpty(deviceDataGraphVoList1)){
            for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVoList1) {
                if (CollectionUtil.isNotEmpty(deviceDataGraphVoList)){
                    for (DeviceDataGraphVo dataGraphVo : deviceDataGraphVoList) {
                        if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())){
                            deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                        }
                    }
                }
            }
        }
        return deviceDataGraphVoList1;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        List<DeviceDataGraphVo> deviceDataGraphVoList1 = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));
        if (CollectionUtil.isNotEmpty(deviceDataGraphVoList1)){
            for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVoList1) {
                if (CollectionUtil.isNotEmpty(deviceDataGraphVoList)){
                    for (DeviceDataGraphVo dataGraphVo : deviceDataGraphVoList) {
                        if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())){
                            deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                        }
                    }
                }
            }
        }
        return deviceDataGraphVoList1;
    }
}
